﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using WeChatRobotSDKCommon.Exceptions;
using WeChatRobotSDKModel.Enum;
using WeChatRobotSDKModel.Model;

namespace WeChatRobotSDKServer
{
    public class WeChatRobotServer
    {
        public WeChatSdkInit WeChat;
        public WeChatRobotServer(WeChatSdkInit weChatSdkInit)
        {
            WeChat = weChatSdkInit;
        }
        //public WeChatRobotServer(WeChatSdkInit _weChat)
        //{
        //    WeChat = _weChat;
        //}


        #region 打开微信--注入DLL
        /// <summary>
        /// 打开微信
        /// </summary>
        public void OpenWechat()
        {
            try
            {
                var ret = WeChat.OpenWechat();
                var list = Process.GetProcessesByName("WeChat");
                if (list.Length > 0)
                    ret = list[0].Id;
                if (ret == 0) return;
                if (!WeChat.IsInjected(ret))
                {
                    WeChat.InjectDll(WeChat.pid);
                }
            }
            catch (Exception ex)
            {
                //ShowLog($"[异常]{ex.Message}");
            }

        }
        #endregion


        #region 获取登录用户
        /// <summary>
        /// 获取登录用户
        /// </summary>
        /// <returns></returns>
        public User GetLoginUser()
        {
            int pid = WeChat.pid;
            var data = new
            {
                Code = Command.Cmd_GetUser
            };
            var respons = WeChat.SendData(pid, Command.Cmd_GetUser);
            if (respons.Error != 0) throw new QNException((uint)respons.Error, respons.Message);
            return respons.Data?.ToObject<User>();
        }
        #endregion


        #region 检查微信是否登录
        /// <summary>
        /// 检查微信是否登录
        /// </summary>
        /// <returns></returns>
        public bool CheckLoginState()
        {
            int pid = WeChat.pid;
            var result = WeChat.SendData(pid, Command.Cmd_CheckLoginStatus);
            var b = result.Data["Status"]?.ToObject<bool>();
            if (b.HasValue) return b.Value;
            return false;
        }

        #endregion

        #region 获取联系人
        /// <summary>
        /// 获取联系人
        /// </summary>
        public void GetContactList()
        {
            int pid = WeChat.pid;
            var data = new
            {
                Code = Command.Cmd_ContactList
            };
            WeChat.SendData(pid, data);
        }
        #endregion

        #region 修改群组名称
        /// <summary>
        /// 修改群组名称
        /// </summary>
        /// <param name="roomId"></param>
        /// <param name="roomName"></param>
        public void SetRoomName(string roomId, string roomName)
        {
            int pid = WeChat.pid;
            var data = new
            {
                Code = Command.Cmd_SetRoomName,
                ChatRoomId = roomId,
                ChatRoomName = roomName
            };
            WeChat.SendData(pid, data);
        }


        #endregion

        #region 发送消息

        /// <summary> 
        /// 发送文本消息 无返回
        /// </summary>
        /// <param name="wxid">接收消息的Wxid</param>
        /// <param name="message">消息内容</param>
        public void SendTextMessage( string wxid, string message)
        {
            int pid = WeChat.pid;
            var data = new
            {
                Code = Command.Cmd_SendTextMessage,
                WxId = wxid,
                Message = message
            };
            WeChat.SendData(pid, data);
        }


        /// <summary> 
        /// 发送图片消息
        /// </summary>
        /// <param name="wxid">接收消息的Wxid</param>
        /// <param name="message">消息内容</param>
        public void SendImageMsg(string wxid, string filePath)
        {
            int pid = WeChat.pid;
            var data = new
            {
                Code = Command.Cmd_SendImageMessage,
                WxId = wxid,
                FilePath = filePath
            };
            WeChat.SendData(pid, data);
        }
        /// <summary>
        /// 发送@消息【群】
        /// </summary>
        /// <param name="chatRoomId"></param>
        /// <param name="memberId"></param>
        /// <param name="memberNick"></param>
        /// <param name="message"></param>
        public void SendChatroomAtMsg(string chatRoomId, string memberId,string memberNick,string message)
        {
            int pid = WeChat.pid;
            var data = new
            {
                Code = Command.Cmd_SendAtMsg,
                ChatRoomId = chatRoomId,
                MemberId = memberId,
                MemberNick = memberNick,
                Message = message,
            };
            WeChat.SendData(pid, data);
        }

        
        #endregion


    }
}
